﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using tju.cme.BookStore2024.webapi.Models.Tools;
using tju.cme.BookStore2024.webapi.Models.ViewModels.Novel;
using tju.cme.BookStore2024.webapi.Services;

namespace tju.cme.BookStore2024.webapi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]

    public class NovelController(NovelServices services):ControllerBase
    {
        /// <summary>
        /// 获得所有小说
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetAll")]
        [AllowAnonymous]
        public async Task<IActionResult> GetAll()
        {
            return Ok(new ReturnMessage(await services.GetAllNovelsAsync()));
        }

        /// <summary>
        /// 根据小说类型获取小说
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        [HttpGet("GetAllByType/{type}")]
        public async Task<IActionResult> GetAllByType([FromRoute] string type)
        {
            return Ok(new ReturnMessage(await services.GetNovelsByTypeAsync(type)));
        }



        /// <summary>
        /// 获得所有上架的小说
        /// </summary>
        /// <returns></returns>
        [HttpGet("GetOnShelf")]
        public async Task<IActionResult> GetOnShelf()
        {
            return Ok(new ReturnMessage(await services.GetOnShelfAsync()));
        }



        /// <summary>
        /// 根据作者id，返回该作者的所有小说
        /// </summary>
        /// <returns></returns>
        [Authorize(Roles = "Author")]
        [HttpGet("GetAllByAuthorId")]
        public async Task<IActionResult> GetAllByAuthorId()
        {
            return Ok(new ReturnMessage(await services.GetNovelsByAuthorIdAsync(GetAuthorizedInfo.GetId(User))));
        }



        [Authorize(Roles = "Admin")]
        [HttpGet("GetNovelsByAdminId")]
        public async Task<IActionResult> GetNovelsByAdminId()
        {
            return Ok(new ReturnMessage(await services.GetNovelsByAdminIdAsync(GetAuthorizedInfo.GetId(User))));
        }


        /// <summary>
        /// 创建小说
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        //[Authorize(Roles = "Author")]
        [HttpPost("Create/{authorId}")]
        public async Task<IActionResult> Greate(NovelCreateModel model, [FromRoute]Guid authorId)
        {
            await services.CreateNovelAsync(model, authorId);
            return Ok(new ReturnMessage() { IsSuccess = true });
        }




        /// <summary>
        /// 小说提交至管理员审核
        /// </summary>
        /// <param name="novelId"></param>
        /// <returns></returns>
        [Authorize(Roles = "Author")]
        [HttpPost("UpToAdmin/{novelId}")]
        public async Task<IActionResult> UpToAdmin([FromRoute] Guid novelId)
        {
            await services.UpToAdminAsync(novelId ,GetAuthorizedInfo.GetId(User));
            return Ok(new ReturnMessage() { IsSuccess = true });
        }


        /// <summary>
        /// 管理员审核已提交小说
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        //[Authorize(Roles = "Admin")]
        [HttpPost("Examine/{adminId}")]
        public async Task<IActionResult> Examine([FromBody] NovelExamineModel model , [FromRoute] Guid adminId)
        {
            await services.ExamineNovelAsync(model, adminId);
            return Ok(new ReturnMessage() { IsSuccess = true });
        }



        /// <summary>
        /// 管理员将审核通过的小说上传到书城
        /// </summary>
        /// <param name="novelId"></param>
        /// <returns></returns>
        [Authorize(Roles = "Admin")]
        [HttpPost("UpToBookStore/{novelId}")]
        public async Task<IActionResult> UpToBookStore([FromRoute] Guid novelId)
        {
            await services.UpToBookStoreAsync(novelId, GetAuthorizedInfo.GetId(User));
            return Ok(new ReturnMessage() { IsSuccess = true });
        }
    }
}
